package cn.edu.tsinghua.smxq.mobilecity.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class QueryLastFivePricesServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312"); 
		output = response.getWriter(); 
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			result = sql.executeQuery("SELECT * FROM foodprice WHERE foodId='" +foodId+
					"' AND cityId='"+cityId+"'");
			
			String[] prices = new String[]{"0","0","0","0","0"};
			int flag=0;
			while (result.next()) { // process results one row at a time
				//userIdValue1,cityIdValue1,foodIdValue1,priceValue1,priceDateValue1
				
		        prices[flag]=result.getString(4);
		        flag++;
		        flag=flag%5;
		      }
			for(int i=0;i<5;i++)
			{
				output.write(prices[flag]+",");
				flag++;
				flag=flag%5;
			}
			sql.close();
			con.close();
			
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());
			
			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		PrintWriter output;
		ResultSet result;
		response.setContentType("text/html; charset=gb2312"); 
		output = response.getWriter(); 
		String cityId = request.getParameter("cityId");
		String foodId = request.getParameter("foodId");
		
		String time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); 
		try {
			Connection con = DataBaseDriver.getConnection();
			Statement sql = con.createStatement();
			result = sql.executeQuery("SELECT * FROM foodprice WHERE foodId='" +foodId+
					"' AND cityId='"+cityId+"'");
			
			String[] prices = new String[]{"0","0","0","0","0"};
			int flag=0;
			while (result.next()) { // process results one row at a time
				//userIdValue1,cityIdValue1,foodIdValue1,priceValue1,priceDateValue1
				
		        prices[flag]=result.getString(4);
		        flag++;
		        flag=flag%5;
		      }
			for(int i=0;i<5;i++)
			{
				output.write(prices[flag]+",");
				flag++;
				flag=flag%5;
			}
			sql.close();
			con.close();
			
		} catch (java.lang.ClassNotFoundException e) {
			System.err.println("ClassNotFoundException:" + e.getMessage());
			
			output.write("failure");
		} catch (SQLException ex) {
			System.err.println("SQLException:" + ex.getMessage());
			output.write("failure");
		}
		
	}
}
